'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index () {
    console.log(123)
    const { ctx } = this;
    const { id } = ctx.query
    await ctx.render('index.html', {
      title: 'test egg'
    })
  }
  // 获取用户信息
  async user () {
    const { ctx } = this
    const res = await ctx.service.home.user()
    ctx.body = res
  }
  async addUser () {
    const { ctx } = this
    const { name } = ctx.request.body
    try {
      const result = await ctx.service.home.addUser(name)
      ctx.body = {
        code: 200,
        msg: '添加成功',
        data: null
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '添加失败',
        data: null
      }
    }
  }
  async editUser () {
    const { ctx } = this
    const { id, name } = ctx.request.body
    try {
      const result = await ctx.service.home.editUser(id, name)
      ctx.body = {
        code: 200,
        msg: '添加成功',
        data: null
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '添加失败',
        data: null
      }
    }
  }

  async deleteUser () {
    const { ctx } = this
    const { id, name } = ctx.query
    try {
      const result = await ctx.service.home.deleteUser(id)
      ctx.body = {
        code: 200,
        msg: '删除成功',
        data: null
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '删除失败',
        data: null
      }
    }
  }

}

module.exports = HomeController;
